package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.service.GroupwithService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * (Group)表控制层
 *
 * @author makejava
 * @since 2020-07-31 14:25:08
 */
@Controller
@ResponseBody
@RequestMapping("usergroup")
public class UsergroupController {
    @Autowired
    GroupwithService groupwithService;

    @ApiOperation("搜索用户所在圈子")
    @RequestMapping(value = "/getUserGroup", method = {RequestMethod.GET})
    public BaseModel getUserGroup(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id,
                                  @ModelAttribute PageQueryModel pageQueryModel
    ) throws Exception {
        BaseModel model = new BaseModel();
        groupwithService.queryallbyuser_id(user_id, pageQueryModel, model);
        return model;
    }

    @ApiOperation("用户加入圈子")
    @RequestMapping(value = "/joinGroup", method = {RequestMethod.PUT})
    public BaseModel joinClass(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id,
                               @ApiParam("圈子id") @RequestParam(value = "group_id", required = true) int group_id
    ) throws Exception {
        BaseModel model = new BaseModel();
        groupwithService.joingroup(user_id, group_id, model);
        return model;
    }

    @ApiOperation("用户退出圈子")
    @RequestMapping(value = "/quitGroup", method = {RequestMethod.DELETE})
    public BaseModel quitGroup(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id,
                               @ApiParam("圈子id") @RequestParam(value = "group_id", required = true) int group_id
    ) throws Exception {
        BaseModel model = new BaseModel();
        groupwithService.quitgroup(user_id, group_id, model);
        return model;
    }

    @ApiOperation(value = "检查用户是否加入班级")
    @RequestMapping(value = "/checkUserClass", method = {RequestMethod.GET})
    public BaseModel checkUserClass(
            @ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id,
            @ApiParam("圈子id") @RequestParam(value = "group_id", required = true) int group_id
    ) {
        BaseModel model = new BaseModel();
        try {
            groupwithService.checkUserGroup(user_id, group_id, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }
}